<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AppUser;
use Illuminate\Http\Request;

class ResltfulApiController extends Controller
{
    /**
     * @param Request $request
     * @return array
     * User: Mr.zhao
     * Date: 2021/5/20 14:14
     * 获取openid
     */
    public  function getOpenId(Request $request)
    {
        //获取登录code
        $code = $request->get('code');
        if (!empty($code)) {
            $appId = "wx6a1f9cabf0e31a13";
            $keyId = "afd9c59c03864467517280a7399e81b0";
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appId."&secret=".$keyId."&js_code=".$code."&grant_type=authorization_code";
            $result = $this->geturl($url);
            if (!isset($result['errcode'])) {
                $openid = $result['openid'];
                $user = AppUser::where(['openid' => $openid])->first();
                if (!$user) {
                    AppUser::create(['openid' => $openid]);
                    return ['code' => 200, 'msg' => '请求成功', 'data' => $openid];
                } else {
                    return ['code' => 500, 'msg' => '请求失败', 'data' => []];
                }
            }
        }else{
            return ['code'=>500,'msg'=>'code不存在','data'=>[]];
        }
    }
    function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }

    /**
     * @param Request $request
     * @return array
     * User: Mr.zhao
     * Date: 2021/5/20 17:12
     * 完善资料
     */
    public  function perfect(Request $request)
    {
        //接受所有用户信息
       $data = $request->all();
       $dataAll = $data['userData'];
       //组装数组
       $row = [
           'nickName'=>$dataAll['nickName'],
           'avatarUrl'=>$dataAll['avatarUrl'],

       ];
       $openid = $data['openid'];
       //根据openid来修改用户信息
        $res = AppUser::where(['openid'=>$openid])->update($row);
        //修改完后查询出一条信息方便后边使用
        $res_one = AppUser::where(['openid'=>$openid])->first();
        if ($res == 1){
            return ['code'=>200,'msg'=>'ok','data'=>$res_one];
        }else{
            return ['code'=>500,'msg'=>'no','data'=>[]];
        }
    }

    /**
     * @param Request $request
     * @return array
     * User: Mr.zhao
     * Date: 2021/5/21 14:41
     * 修改资料
     */
     public  function updateUserInfo(Request $request)
     {
         //接受所有数据
         $dataAll = $request->all();
         $userInfo  = $dataAll['userInfo'];
         //组装数组
         $row = [
           'avatarUrl'=>$userInfo['avatarUrl'],
             'trueName'=>$userInfo['trueName'],
             'phone'=>$userInfo['phone'],
             'gender'=>$dataAll['gender']
         ];
         //获取openid
         $openid = $dataAll['openid'];
         //修改数据
         $updateRes = AppUser::where(['openid'=>$openid])->update($row);
         //修改成功查询出这条数据
         $one_data = AppUser::where(['openid'=>$openid])->first();
         if ($updateRes == 1){
             return ['code'=>200,'msg'=>'修改成功','data'=>$one_data];
         }else{
             return  ['code'=>500,'msg'=>'修改失败','data'=>[]];
         }
     }
    /**
     * @param Request $request
     * @return array
     * User: Mr.zhao
     * Date: 2021/5/20 20:37
     * 图片上传
     */
    public  function uploadsFile(Request $request)
    {
        //接受要上传的图片
        $url = $request->file('avatarUrl')->store('','uploads');

        if ($url){
            $imgPath = "http://www.rikao.com/uploads/imagePath/" . $url;

            return ['code'=>200,'msg'=>'上传成功','data'=>$imgPath];
        }else{
            return  ['code'=>500,'msg'=>'上传失败','data'=>[]];
        }

    }
}
